package com.ll.wechat.chatRecord.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.ll.wechat.R;
import com.ll.wechat.chat.widget.FaceUtil;
import com.ll.wechat.chatRecord.bean.ChatRecord;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 聊天记录列表的适配器
 * 
 * @ClassName: ChatRecordListAdapter
 * @author yeliangliang
 * @date 2015-8-5 上午10:28:44
 */
public class ChatRecordListAdapter extends BaseAdapter {

	private ArrayList<ChatRecord> mList;
	private Context mContext;
	private LayoutInflater inflater;
	private HolderView holderView;
	private FaceUtil faceUtil;
	public ChatRecordListAdapter(Context context, ArrayList<ChatRecord> list) {
		this.mContext = context;
		this.mList = list;
		inflater = LayoutInflater.from(mContext);
		faceUtil = FaceUtil.getInstance(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_chat_record_list, null);
			holderView = new HolderView();
			holderView.tv_name = (TextView) convertView.findViewById(R.id.tv_chat_name);
			holderView.tv_record = (TextView) convertView.findViewById(R.id.tv_chat_record);
			holderView.tv_time = (TextView) convertView.findViewById(R.id.tv_chat_time);
			holderView.img_chat_head = (ImageView) convertView.findViewById(R.id.img_chat_head);
			convertView.setTag(holderView);
		} else {
			holderView = (HolderView) convertView.getTag();
		}
		ChatRecord chatRecord = mList.get(position);
		if (chatRecord.getStaues().equals("0")) {//已读
			holderView.img_chat_head.setImageResource(R.drawable.icon_head);
		}else {//未读
			holderView.img_chat_head.setImageResource(R.drawable.icon_head_count);
		}
		holderView.tv_name.setText(chatRecord.getName());
		holderView.tv_record.setText(faceUtil.matchingString(chatRecord.getContent(),false));
		holderView.tv_time.setText(displayTime(chatRecord.getTime()));
		return convertView;
	}

	private class HolderView {
		private TextView tv_name;
		private TextView tv_record;
		private TextView tv_time;
		private ImageView img_chat_head;
	}

	/**
	 * 计算最近记录中应该显示的时间
	 * 
	 * @param time
	 * @return
	 * @author yeliangliang
	 * @date 2015-8-6 下午6:09:02
	 * @version V1.0
	 * @return String
	 */
	private String displayTime(String time) {
		long ti = Long.valueOf(time);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String t1 = dateFormat.format(ti);
		String t2 = dateFormat.format(System.currentTimeMillis());
		if (t1.equals(t2)) {
			// 时间为今天
			dateFormat = new SimpleDateFormat("HH:mm");
			return dateFormat.format(ti);
		}
		dateFormat = new SimpleDateFormat("yyyy");
		String t3 = dateFormat.format(ti);
		String t4 = dateFormat.format(System.currentTimeMillis());
		if (t3.equals(t4)) {
			// 今年
			dateFormat = new SimpleDateFormat("MM-dd");
			return dateFormat.format(ti);
		} else {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(ti);
		}

	}
}
